//lastImgIsUpload:上一张图片是否还在传,maxUploadImgNum:最大上传图片数
let nowDate = new Date(),lastImgIsUpload = false,maxUploadImgNum = 5,imgUploadPath = 'feedback/';
let month = nowDate.getMonth() + 1;
month = month < 10 ? '0' + month : month;
let yearMonth = nowDate.getFullYear() + '' + month;
$(function(){
    $('.btn-feedback').click(function () {
        saveFeedBack(this);
    });
    let fbContent = $('#fbContent');
    fbContent.keyup(function () {
        let fbContent = V('fbContent');
        let contentLength = fbContent.length, wordNum = 0;
        if (contentLength > 300){
            fbContent.val(fbContent.substring(0,300));
        } else {
            wordNum = 300 - contentLength;
        }
        $('#wordNum').text(wordNum);
    });
});
function showError(s) {
    $('#errorMsg').html(s);
}
function saveFeedBack(btnFb) {
    let fbContact = V('fbContact'), fbContent = V('fbContent'),btnFeedback = btnFb;
    if (isNull(fbContact)){
        showError('联系方式不能为空');
        return;
    }
    //如果包含中文
    if (/.*[\u4e00-\u9fa5]+.*$/.test(fbContact)){
        showError('请填写正确的手机号码或邮箱');
        return;
    }
    let firstWord = fbContact.substring(0,1);
    //如果是手机号码
    if (firstWord == '1' && !phoneNumberIsRight(fbContact) || (!isNaN(firstWord) && firstWord != '1' && /.*[a-zA-Z]+.*$/.test(fbContact))){
        showError('手机号码不正确');
        return;
    }
    if ((/.*[a-zA-Z]+.*$/.test(fbContact) && fbContact.indexOf('@') <= 0) || (fbContact.indexOf('@') > 0 && !checkEmail(fbContact))){
        showError('邮箱不正确');
        return;
    }
    if (isNull(fbContent)){
        showError('反馈内容不能为空');
        return;
    }
    $(btnFeedback).text('正在提交...').attr('disabled',true);
    let uploadImgPath = '';
    $('.ul-img-list .upload-img').each(function () {
        uploadImgPath += yearMonth + '\/' + $(this).attr('data-imgName') + '|';
    });
    if (uploadImgPath.length > 1){
        uploadImgPath = uploadImgPath.substring(0,uploadImgPath.length - 1);
    }
    $.ajax({
        type : "POST",
        url : '/index/feedback/saveFeedBack',
        data : {
            fbContact: fbContact,
            fbType: $('input:radio[name="fbType"]:checked').val(),
            imgPathName: uploadImgPath,
            fbContent: fbContent
        },
        dataType : 'json',
        success : function(result) {
            $(btnFeedback).text('提交反馈').attr('disabled',false);
            if (result == '2') {
                showError('反馈内容不能为空');
            }else if (result == '0') {
                showError('提交成功，非常感谢您的反馈');
                $('#fbContent').val('');
            }else {
                showError('提交失败，请稍候再试');
            }
        },
        error: function() {
            showError('系统繁忙，请您稍候再试');
            $(btnFeedback).text('提交反馈').attr('disabled',false);
        }
    });
}
function addImg() {
    if (lastImgIsUpload){
        showError('上一张图片还未传完，请稍候再传');
        return;
    }
    $('#uploadImg').click();
}

/**
 * 添加图片集
 */
function addImgList() {
    let imgReader = new FileReader();
    let imgFile = document.getElementById('uploadImg').files[0];
    if (!/\/(?:jpeg|jpg|png)/i.test(imgFile.type)){
        showError('请选择图片再上传');
        return;
    }
    let imgFormData = new FormData();
    // 通过append向form对象添加数据
    imgFormData.append('file', imgFile);
    //传年月参数
    //imgFormData.append('yearMonth', yearMonth);
    imgFormData.append('imgPath',imgUploadPath + yearMonth);
    $.ajax({
        url: '/index/feedback/uploadFile',
        type: 'POST',
        cache: false, //上传文件不需要缓存
        data: imgFormData,
        processData: false, // 不要去处理发送的数据
        contentType: false, // 不要去设置Content-Type请求头
        success: function(uploadImgData){
            lastImgIsUpload = false;
            imgReader.readAsDataURL(imgFile);
            imgReader.onload = function (e) {
                let imgName = uploadImgData.fileName;
                $('.ul-img-list').prepend('<li data-imgName="' + imgName + '" class="upload-img li-img-list-item" onmouseover="showDeleteImg(this)" onmouseout="hideDeleteImg(this)">' +
                '   <img width="96" height="96" src="' + e.target.result + '">' +
                '   <div class="hide">' +
                '       <img width="32" height="32" onclick="deleteImg(this,\'' + imgName + '\')" src="/staticFile/images/delete.png">' +
                '   </div>' +
                '</li>');
                //上传图片数量
                let uploadImgNum = $('.ul-img-list .upload-img').length;
                if (uploadImgNum >= maxUploadImgNum){
                    $('#add').hide();
                }
            };

        },
        error: function(){
            lastImgIsUpload = false;
            showError(imgFile.name + '图片上传失败，请重新选择');
        }
    });
}
//显示删除小图标
function showDeleteImg(delDiv) {
    $(delDiv).children('div').removeClass('hide');
}
//隐藏删除小图标
function hideDeleteImg(delDiv) {
    $(delDiv).children('div').addClass('hide');
}
/**
 * 删除图片
 * @param delImgBtn 删除图标img对象
 * @param delImgName 要删除的图标名称
 */
function deleteImg(delImgBtn,delImgName) {
    $(delImgBtn).parents('div').parents('li').remove();
    $('#add').show();
    $.ajax({
        type : "POST",
        url : '/index/feedback/deleteFile',
        data : {
            fileName: delImgName,
            imgPath: imgUploadPath + yearMonth
        },
        dataType : 'json',
        success : function(r) {
        }
    });
}